package com.tian.service.impl;

import com.tian.common.CommonResult;
import com.tian.dto.exchange.CheckExchangeCodeReqDto;
import com.tian.dto.exchange.CheckExchangeCodeRespDto;
import com.tian.entity.ChargeCoupon;
import com.tian.mapper.ChargeCouponMapper;
import com.tian.service.ExchangeCodeValidateService;
import com.tian.strategy.CalculateDiscountAmountService;
import com.tian.strategy.CalculateDiscountAmountStrategy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * {@code @description:} 正常验兑换码校验实现类
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-11-24 19:51
 * {@code @version:} 1.0
 */
@Service("exchangeCodeValidateService")
public class ExchangeCodeValidateServiceImpl implements ExchangeCodeValidateService {
    @Resource
    private ChargeCouponMapper chargeCouponMapper;

    @Override
    public CommonResult<CheckExchangeCodeRespDto> validateCode(CheckExchangeCodeReqDto checkExchangeCodeReqDto) {
        ChargeCoupon chargeCoupon = chargeCouponMapper.selectByPrimaryKey(checkExchangeCodeReqDto.getCouponId());

        CalculateDiscountAmountService calculateDiscountAmountService = CalculateDiscountAmountStrategy.getCalculateDiscountAmountService(chargeCoupon.getUseType());
        BigDecimal couponAmount = calculateDiscountAmountService.calculateDiscountAmount(checkExchangeCodeReqDto.getAmount(), chargeCoupon.getFaceValue());

        CheckExchangeCodeRespDto checkExchangeCodeRespDto = new CheckExchangeCodeRespDto();
        checkExchangeCodeRespDto.setCouponAmount(couponAmount);
        checkExchangeCodeRespDto.setCode(checkExchangeCodeReqDto.getCode());
        return CommonResult.success(checkExchangeCodeRespDto);
    }
}
